package LeetCode;

/**
 * @author zhanghao
 * @create 2023-09-06 11:38
 *
 *
 * 最长公共前缀
 */
public class code14_longestCommonPrefix {

    public static String longestCommonPrefix(String[] strs){
        if(strs == null || strs.length == 0){
            return "";
        }
        char[] chars = strs[0].toCharArray();
        int min = Integer.MAX_VALUE;
        for (String str : strs) {
            char[] tempChar = str.toCharArray();
            int index = 0;
            while (index < tempChar.length && index < chars.length){
                if(chars[index] != tempChar[index]){
                    //两个字符不相等
                    break;
                }
                index++;
            }
            min = Math.min(index,min);
            if(min == 0){
                return "";
            }
        }
        return strs[0].substring(0,min);
    }
}
